package me.firstapp.repository.shop.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.Product;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.ProductRepository;

@Repository
public class ProductRepositoryImpl extends EntityRepositoryImpl<Product> implements ProductRepository {

	@Override
	protected Class<Product> getPersistentClass() {
		return Product.class;
	}

	@Override
	public Page<Product> findProductPage(Integer pageNo, Integer pageSize, Long categoryId, Integer disable, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");

		if (categoryId != null) {
			sql.append(" AND bean.category.id=:categoryId");
			parameters.put("categoryId", categoryId);
		}
		if (disable != null) {
			sql.append(" AND bean.disable=:disable");
			parameters.put("disable", disable);
		}
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" ORDER BY bean.sort ASC");
		return super.queryForPage(Product.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Product findProductById(Long productId, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		sql.append(" AND bean.id=:productId");
		parameters.put("productId", productId);
		
		List<Product> results = super.query(Product.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public Page<Product> findProductPageByParams(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Date startTime, Date endTime, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					if ("categoryId".equals(key)) {
						String _value = (String) value;
						Long categoryId = Long.valueOf(_value);
						sql.append(" AND bean.category.id =:value ");
						parameters.put("value", categoryId);
					} else if ("categoryName".equals(key)) {
						sql.append(" AND bean.category.name LIKE :value ");
						parameters.put("value", "%" + value + "%");
					} else {
						sql.append(" AND bean." + key + " LIKE :value ");
						parameters.put("value", "%" + value + "%");
					}
				}
			}
		}

		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}

		if (_startTime != null) {
			sql.append(" AND bean.addTime >=:startTime ");
			parameters.put("startTime", _startTime);
		}
		if (_endTime != null) {
			sql.append(" AND bean.addTime <=:endTime");
			parameters.put("endTime", _endTime);
		}
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			if ("categoryName".equals(sortParam)) {
				sql.append(" ORDER BY  category.name " + sortType);
			} else {
				sql.append(" ORDER BY " + sortParam + " " + sortType);
			}
		}

		return super.queryForPage(Product.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Product findProductByName(String name, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		sql.append(" AND bean.name =:name");
		parameters.put("name", name);

		List<Product> result = super.query(Product.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public List<Product> findProducts(Integer disable, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");

		if (disable != null) {
			sql.append(" AND bean.disable =:disable");
			parameters.put("disable", disable);
		}
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		return super.query(Product.class, sql.toString(), parameters);
	}

}
